package com.siwei.mes.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.system.DictValue;
import com.siwei.mes.mapper.system.DictValueMapper;
import com.siwei.mes.service.system.DictValueService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 字典值表(DictValue)表服务实现类
 *
 * @author linzi
 * @since 2023-11-25 22:22:21
 */
@Service
public class DictValueServiceImpl implements DictValueService {
    @Resource
    private DictValueMapper dictValueMapper;

    @Override
    public Long create(DictValue entity) {
        dictValueMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(DictValue entity) {
        DictValue dictValue = dictValueMapper.selectByPrimaryKey(entity.getId());
        assertTrue(dictValue == null, "字典值表信息不存在");
        dictValueMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        DictValue dictValue = dictValueMapper.selectByPrimaryKey(id);
        assertTrue(dictValue == null, "字典值表信息不存在");
        dictValueMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<DictValue> getPage(PageQuery<DictValue, DictValue> query) {
        DictValue searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> dictValueMapper.getList(searchParam));
    }

    @Override
    public DictValue loadById(Long id) {
        return dictValueMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<DictValue> getAll(DictValue entity) {
        return dictValueMapper.getList(entity);
    }

    @Override
    public Map<String, List<DictValue>> batchDictValue(DictValue dictValue) {
        List<DictValue> dataList = dictValueMapper.getList(dictValue);
        return dataList.stream().collect(Collectors.groupingBy(DictValue::getDictCode));
    }
}
